package org.example.demo;

import org.example.crawler.constant.RegexConstant;
import us.codecraft.webmagic.Page;
import us.codecraft.webmagic.Site;
import us.codecraft.webmagic.Spider;
import us.codecraft.webmagic.pipeline.JsonFilePipeline;
import us.codecraft.webmagic.processor.PageProcessor;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * @Author lyz
 * @Date 2025/3/24 14:18
 */
public class WebMagicDemo implements PageProcessor {


    private Site site = Site.me().setRetryTimes(3).setSleepTime(100).setTimeOut(1000000);

    static {
        //System.setProperty("javax.net.debug", "all");
        System.setProperty("https.protocols", "TLSv1,TLSv1.1,TLSv1.2");
    }

    public static void main(String[] args) {
        String str = "中 (AV:L/AC:L/Au:S/C:C/I:C/A:C)";
        Pattern pattern = Pattern.compile(RegexConstant.chinese);
        Matcher matcher = pattern.matcher(str);
        if (matcher.find()) {
            System.out.println(matcher.group(0));
        }

//        Spider.create(new WebMagicDemo())
//                .addUrl("https://github.com/code4craft")
//                .thread(5)
//                .run();
    }

    @Override
    public void process(Page page) {
        page.addTargetRequests(page.getHtml().links().regex("(https://github\\.com/\\w+/\\w+)").all());
        page.putField("author", page.getUrl().regex("https://github\\.com/(\\w+)/.*").toString());
        page.putField("name", page.getHtml().xpath("//h1[@class='entry-title public']/strong/a/text()").toString());
        if (page.getResultItems().get("name") == null) {
            //skip this page
            page.setSkip(true);
        }
        page.putField("readme", page.getHtml().xpath("//div[@id='readme']/tidyText()"));
    }

    @Override
    public Site getSite() {
        return site;
    }
}
